package com.shop.admin.controller;

import com.shop.Result.LdbzResult;
import com.shop.api.feign.AdminItemFeign;
import com.shop.pojo.LdbzItem;
import com.shop.utils.ConvertUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/admin/item")
public class AdminItemController {

    private static final Logger logger = LoggerFactory.getLogger(AdminItemController.class);

    @Autowired
    private AdminItemFeign adminItemFeign;

    //图片的URL路径
    @Value("${redisKey.nginxImage.url.key}")
    private String INDEX_NGINX_IMAGE_URL;

    @ApiOperation(value="产品页面跳转", notes="跳转到产品页面")
    @RequestMapping(value="/indexItem" , method = RequestMethod.GET)
    public String indexItem() {
        logger.debug("go to indexItem");
        return "indexItem";
    }

    @ApiOperation(value="新增页面跳转", notes="跳转到产品新增页面")
    @RequestMapping(value="/addItem" , method = RequestMethod.GET)
    public String addItem(Model model) {
        logger.debug("go to indexItem_add ");
        model.addAttribute("nginxImage" , INDEX_NGINX_IMAGE_URL);
        return "indexItem_add";
    }

    @ApiOperation(value="修改页面跳转", notes="跳转到产品修改页面")
    @ApiImplicitParam(name = "id", value = "产品id", required = true, dataType = "long",paramType = "path")
    @RequestMapping(value="/editItem/{id}" , method = RequestMethod.GET)
    public String editItem(@PathVariable("id")long id, Model model){
        logger.debug("go to indexItem_edit id : {}",id);
        LdbzResult ldbzResult = adminItemFeign.editItem(id);
        model.addAttribute("item",ldbzResult.getData());
        model.addAttribute("nginxImage",INDEX_NGINX_IMAGE_URL);
        return "indexItem_edit";
    }

    @ApiOperation(value="获取商品信息", notes="根据条件获取商品信息")
    @ApiImplicitParam(name = "entity", value = "LdbzItem实体", required = true, dataType = "LdbzItem")
    @ResponseBody
    @RequestMapping(value="/getItemList" , method = RequestMethod.POST)
    public LdbzResult getItemList(LdbzItem entity){
        logger.debug("go to getItemList : {}",entity);
        entity.setTitle(ConvertUtils.getKey(entity.getTitle()));
        entity.setCode(ConvertUtils.getKey(entity.getCode()));
        return adminItemFeign.getItemList(entity);
    }

    @ApiOperation(value="分页获取产品", notes="根据实体LdbzItem分页获取商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "LdbzItem实体", required = true, dataType = "LdbzItem"),
            @ApiImplicitParam(name = "page", value = "查询的页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页显示的条数", required = true, dataType = "int")
    })
    @ResponseBody
    @RequestMapping(value="/getItemPage" , method = RequestMethod.POST)
    public LdbzResult getItemPage(LdbzItem entity,int page,int limit){
        entity.setTitle(ConvertUtils.getKey(entity.getTitle()));
        return adminItemFeign.getItemPage(entity,page,limit);
    }

    @ApiOperation(value="删除商品", notes="根据id物理删除一条商品")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "String",paramType = "path")
    @ResponseBody
    @RequestMapping(value="/deleteByKey/{id}" , method = RequestMethod.POST)
    public LdbzResult deleteByKey(@PathVariable("id") String id){
        return adminItemFeign.deleteByKey(id);
    }

    @ApiOperation(value="新增商品", notes="创建一条新的商品")
    @ApiImplicitParam(name = "entity", value = "LdbzItem实体", required = true, dataType = "LdbzItem")
    @ResponseBody
    @RequestMapping(value="/insertByEntity" , method = RequestMethod.POST)
    public LdbzResult insertByEntity(LdbzItem entity){
        entity.setCreated(new Date());
        entity.setCode(String.valueOf(System.currentTimeMillis()));
        return adminItemFeign.insertByEntity(entity);
    }

    @ApiOperation(value="修改商品", notes="根据Id修改一条商品")
    @ApiImplicitParam(name = "entity", value = "LdbzItem实体", required = true, dataType = "LdbzItem")
    @ResponseBody
    @RequestMapping(value="/updateByKey" , method = RequestMethod.POST)
    public LdbzResult updateByKey(LdbzItem entity){
        entity.setUpdated(new Date());
        return adminItemFeign.updateByKey(entity);
    }

    @ApiOperation(value="商品同步到ES", notes="根据商品code同步商品信息到ES")
    @ApiImplicitParam(name = "code", value = "商品编号", required = true, dataType = "long",paramType = "path")
    @ResponseBody
    @RequestMapping(value="/syncItemToSolrByCode/{code}" , method = RequestMethod.POST)
    public LdbzResult syncItemToESByCode(@PathVariable("code") long code){
        return adminItemFeign.syncItemToESByCode(code);
    }
}
